﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;

namespace PixentrationEngine
{
    /// <summary>
    /// Utility class to manage list of high scores.
    /// </summary>
    class Scorecard
    {
        // List of scores sorted in descending
        // order from highest to lowest where
        // index 0 is highest score.
        public List<ScoreEntry> scores;

        public StreamWriter scoresWriter;
        public StreamReader scoresReader;

        public readonly DirectoryInfo DI;
        public readonly string FILE_PATH;
        public readonly string FILE_NAME;
        public readonly string TARGET;



        /// <summary>
        /// Retrieves scores from storage.
        /// This is essentially the initialization method.
        /// </summary>
        public void LoadScores()
        {
            scores = new List<ScoreEntry>();

            if (!File.Exists(TARGET))
            {
                using (scoresWriter = new StreamWriter(File.Create(TARGET)))
                {
                    for (int i = 0; i < 9; i++)
                        scoresWriter.WriteLine("NUL,0," + DateTime.Now);
                }
            }


            using (scoresReader = new StreamReader(TARGET))
            {
                string entry;
                while ((entry = scoresReader.ReadLine()) != null)
                {
                    string[] tokens = entry.Split(',');
                    ScoreEntry se = new ScoreEntry();
                    se.Name = tokens[0];
                    se.Score = Convert.ToInt32(tokens[1]);
                    se.Time = tokens[2];
                    scores.Add(se);
                }
            }

            scores.Sort();

        }

        /// <summary>
        /// Save scores to storage.
        /// </summary>
        public void SaveScores()
        {
            if (!File.Exists(TARGET))
                File.Create(TARGET);
            
            using (scoresWriter = new StreamWriter(TARGET))
            {
                foreach (ScoreEntry s in scores)
                    scoresWriter.WriteLine(s.Name + "," + s.Score + "," + s.Time);
            }
        }

        /// <summary>
        /// Returns scores to client.
        /// </summary>
        /// <returns></returns>
        public List<ScoreEntry> GetScores()
        {
            return scores;
        }


        
        public bool isHighScore(ScoreEntry entry)
        {
            scores.Sort();
            ScoreEntry lastEntry = scores[scores.Count - 1];
            Console.WriteLine("last entry:  " + lastEntry.Score );
            Console.WriteLine("new entry:  " + entry.Score);
            return (entry.Score > lastEntry.Score);
        }


        
        
        /// <summary>
        /// Try to add a new score to the list of high scores.
        /// </summary>
        /// <param name="score">Score to add or reject.</param>
        public bool NewScore(ScoreEntry newEntry)
        {
            if (isHighScore(newEntry))
            {
                scores.Sort();
                Console.WriteLine("New high score:  " + newEntry.Score);
                ScoreEntry lastEntry = scores[scores.Count - 1];
                scores.Remove(lastEntry);
                scores.Add(newEntry);
                Console.WriteLine(scores[scores.Count - 1].Score);
                scores.Sort();
                return true;
            }

            // return true if the score was added
            return false;
        }


        public Scorecard()
        {
            FILE_PATH = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
                + "\\Pixentration";

            DI = Directory.CreateDirectory(FILE_PATH);

            FILE_NAME = "highscores.txt";

            TARGET = FILE_PATH + "\\" + FILE_NAME;
        }

    }

}